/***************************************************
> Copyright (C) 2025 ==LENA== All rights reserved.
> File Name: client.c
> Author: lena
> Mail:lena_li@126.com 
> Created Time: 2025年01月03日 星期五 16时32分43秒
***************************************************/
//客户端接收
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include "cJSON.h"

// regist函数，用于注册，返回注册成功获取的ID，参数为电话号码和服务器地址结构体指针
int regist(char phonenum[20], struct sockaddr_in *seraddr); 

// logout函数，用于注销，电话号码和服务器地址结构体指针注销成功返回1，失败返回-1
int logout(char phonenum[20], struct sockaddr_in *seraddr); 

/*
函数名：recv_data;
返回值：无;
参数：json;
*/
void recv_data(cJSON *json)
{
	cJSON *node = NULL;
    json = cJSON_Parse(result);
    node = cJSON_GetObjectItem(json, "authorization");
    if (node == NULL) 
	{
        printf("can't find authorization\n");
    }
    else if (node->type == cJSON_String) 
	{
        printf("本客户端的ID为%d\n",ID);
		char temp[20] = 0;
		//找到返回位置，没找到返回0；
		char* p = strstr(node->authorization,ID);
		if(p != 0)
		{
			//判断序列号
			
		}
		else
		{
			
		}
        
    }
}

int regist(char phonenum[20], struct sockaddr_in *seraddr) 
{
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        perror("socket");
        exit(-1);
    }

    // 连接服务器
    if (connect(sock, (struct sockaddr *)seraddr, sizeof(struct sockaddr_in)) == -1) {
        perror("connect");
        exit(-1);
    }

    cJSON *json = NULL;
    char type[20] = "register";
    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json, "type", cJSON_CreateString(type));
    cJSON_AddItemToObject(json, "phonenum", cJSON_CreateString(phonenum));
    char *dest = NULL;
    dest = cJSON_Print(json);
    char buf[100] = {0};
    memcpy(buf, dest, strlen(dest));
    // 使用send函数发送数据，因为是TCP协议
    int ret = send(sock, buf, strlen(buf), 0);
    if (ret == -1) {
        printf("send register");
        exit(-1);
    }

    // 接收结果
    char result[100] = {0};
    memset(&result, 0, sizeof(result));
    // 使用recv函数接收数据，因为是TCP协议
    ret = recv(sock, result, sizeof(result), 0);
    if (ret == -1) {
        printf("recv ID");
        exit(-1);
    }

    cJSON *node = NULL;
    json = cJSON_Parse(result);
    node = cJSON_GetObjectItem(json, "ID");
    if (node == NULL) {
        printf("can't find ID\n");
        return -1;
    }
    else if (node->type == cJSON_String) {
        char ID[20];
        strcpy(ID, node->valuestring);
        close(sock);
        return atoi(ID);  // 将ID字符串转换为整数返回，假设ID是数字形式存储的，可按需调整
    }
    close(sock);
    return -1;
}

int logout(char phonenum[20], struct sockaddr_in *seraddr) 
{
    int res = 0;
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        perror("socket");
        exit(-1);
    }

    // 连接服务器
    if (connect(sock, (struct sockaddr *)seraddr, sizeof(struct sockaddr_in)) == -1) {
        perror("connect");
        exit(-1);
    }

    cJSON *json = NULL;
    char type[20] = "logout";
    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json, "type", cJSON_CreateString(type));
    cJSON_AddItemToObject(json, "phonenum", cJSON_CreateString(phonenum));
    char *dest = NULL;
    dest = cJSON_Print(json);
    char buf[100] = {0};
    memcpy(buf, dest, strlen(dest));
    // 使用send函数发送数据
    int ret = send(sock, buf, strlen(buf), 0);
    if (ret == -1) {
        printf("send logout");
        exit(-1);
    }

    // 接收结果
    char result[100] = {0};
    memset(&result, 0, sizeof(result));
    // 使用recv函数接收数据
    ret = recv(sock, result, sizeof(result), 0);
    if (ret == -1) {
        printf("recv res");
        exit(-1);
    }

    cJSON *node = NULL;
    json = cJSON_Parse(result);
    node = cJSON_GetObjectItem(json, "res");
    if (node == NULL) {
        printf("can't find res\n");
        return -1;
    }
    else if (node->type == cJSON_Number) {
        res = node->valueint;
        close(sock);
        return res;
    }
    close(sock);
    return -1;
}


int main(int argc, const char* argv[]) {
    // 创建套接字
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    int ret = 0;
    if (sock == -1) {
        perror("socket");
        exit(-1);
    }

    // 定义地址
    struct sockaddr_in seraddr;
    memset(&seraddr, 0, sizeof(struct sockaddr_in));
    seraddr.sin_family = AF_INET;
    seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    // htons：转化为可以java理解的
    seraddr.sin_port = htons(6868);

    // 注册
    printf("是否注册？\n");
    printf("1---注册\n");
    printf("2---登录\n");
    printf("3---退出\n");
    printf("4---注销\n");
    printf("输入数字进行你的选择\n");
    int val = 0;
    scanf("%d", &val);
	int ID = 0;
    switch (val)
    {
    case 1:
        char phonenum[20];
        printf("请输入你的注册号码\n");
        scanf("%s", phonenum);
        int ID = regist(phonenum, &seraddr);
        // 注册失败处理
        while (ID == -1) {
            printf("请输入新的号码");
            scanf("%s", phonenum);
            ID = regist(phonenum, &seraddr);
        }
        // 注册成功
        printf("%s用户已注册成功,您的登录密码为您的手机号\n", phonenum);
        break;
    case 2:
        if (ID == 0) 
		{
            printf("本客户端尚未注册,请注册后再尝试登录\n");
			break;
		}
        else 
		{
            char temp[20] = {0};
            printf("本客户端的ID为%d\n", ID);
            int tryTimes = 0;  // 记录已经尝试的次数
            while (tryTimes < 3) 
			{
                printf("请输入密码\n");
                scanf("%s", temp);
                if (strcmp(temp, phonenum) == 0)
					{
						printf("登陆成功\n");
						break;  // 登录成功，跳出循环
					}
                else {
                    tryTimes++;
                    if (tryTimes < 3) 
					{
                        printf("密码错误,请重新输入\n");
                    }
                    else 
					{
                        printf("密码错误次数过多，禁止再次输入！\n");
                    }
                }
            }
        }
        break;
    case 3:
        break;
    case 4:
        char temp[20] = {0};
        int res = 0;
        printf("请输入密码\n");
        scanf("%s", temp);
        res = logout(temp, &seraddr);
        if (res == 1) {
            printf("注销成功\n");
        }
        else if (res == -1) {
            printf("注销失败\n");
        }
        break;
    default:
        break;
    }
	char pack[100] = {0};
    memset(&pack, 0, sizeof(pack));
    // 使用recv函数接收数据

    ret = recv(sock,pack, sizeof(pack), 0);
    if (ret == -1)
	{
        printf("recv pack\n");
        exit(-1);
    }
	int res = 0;
	cJSON *json = NULL;
    cJSON *node = NULL;
    json = cJSON_Parse(pack);
    node = cJSON_GetObjectItem(json, "isreplenishment");
    if (node == NULL) {
        printf("can't find isreplenishment\n");
        return -1;
    }
    else if (node->type == cJSON_Number) {
        res = node->valueint;
		{
			if(res == 1)
			{
				
			}
			else if{res == 0}
			{
				//判断授权信息
				recv_data(json);
			}
			
		}
        close(sock);
        return res;
    }
	
    close(sock);
    return 0;
}